import requests
import json
import time



# 结构体时间与次数对应，这样在循环中只执行一次
class Myclass(object):
    class Struct(object):
        def __init__(self, Ttime, cishu):
            self.Ttime = Ttime
            self.cishu = cishu

    def make_struct(self, Ttime, cishu):
        return self.Struct(Ttime, cishu)
myclass = Myclass()
test1 = myclass.make_struct("17:55:15", 0)
test2 = myclass.make_struct("01:30:45", 0)



# 钉钉机器人显示
def getDingMes():
    # files open
    with open('1.txt', 'r', encoding='UTF-8') as file:
        contents = file.read()

    print(ticks_1)
    # 更改为自己的钉钉机器人
    baseUrl = "这里面填写刚刚复制的webhook"

    # please set charset= utf-8
    HEADERS = {
        "Content-Type": "application/json ;charset=utf-8 "
    }
    # 这里的message是你想要推送的文字消息
    message = "@时间：" + ticks + "\n" + contents
    stringBody = {
        "msgtype": "text",
        "text": {"content": message},
        "at": {
            "atMobiles": [""],
            "isAtAll": "false"  # @所有人 时为true，上面的atMobiles就失效了
        }
    }
    MessageBody = json.dumps(stringBody)
    result = requests.post(url=baseUrl, data=MessageBody, headers=HEADERS)
    print(result.text)



if __name__ == '__main__':
    while 1:
        ticks_1 = str(time.strftime("%H:%M:%S"))  # ticks_1 当前时间(时分秒)
        ticks = str(time.strftime("%Y %m %d %H:%M"))  # ticks 当前时间（显示）
        # 满足条件发送
        if ticks_1 == test1.Ttime and test1.cishu == 0:
            getDingMes()
            test1.cishu = test1.cishu + 1
        if ticks_1 == test2.Ttime and test2.cishu == 0:
            getDingMes()
            test2.cishu = test2.cishu + 1
        # 每日重置次数，第二天依旧按点发送
        if ticks_1 == "01:32:00":
            test1.cishu = 0
            test2.cishu = 0
